package com.aua.book.android.xml;

import android.text.Html;
import android.util.TypedValue;
import android.widget.TextView;

public class ZoomHelper {

  public static float zoomIn(AndroidBookActivity app, float size, TextView textView, TextView zoomText) {
    float currentSize = textView.getTextSize();

    if (currentSize + size < Integer.parseInt(app.getString(R.string.maxFontSize))) {
      currentSize += size;
      makeZoom(app, textView, zoomText, currentSize);
    }
    return currentSize;
  }

  public static float zoomOut(AndroidBookActivity app, float size, TextView textView, TextView zoomText) {

    float currentSize = textView.getTextSize();

    if (currentSize - size >= Integer.parseInt(app.getString(R.string.minFontSize))) {
      currentSize -= size;
      makeZoom(app, textView, zoomText, currentSize);

    }
    return currentSize;
  }

  public static float initialSize(AndroidBookActivity app, TextView textView, TextView zoomText) {
    float currentSize = Float.parseFloat(app.getString(R.string.minFontSize));
    makeZoom(app, textView, zoomText, currentSize);

    return Float.parseFloat(app.getString(R.string.minFontSize));
  }

  public static int getZoomP(AndroidBookActivity app, TextView textView) {

    return (int) (100 * textView.getTextSize()) / Integer.parseInt(app.getString(R.string.minFontSize));
  }

  private static void makeZoom(AndroidBookActivity app, TextView textView, TextView zoomText,
      float currentSize) {
    int prevSize = app.getPages().size();
    PageCreator.creat(app, currentSize);
    int newCurrentPage = app.getCurrnetPage() * app.getPages().size() / prevSize;
    app.setCurrnetPage(newCurrentPage);
    textView.setText(Html.fromHtml(app.getPages().get(newCurrentPage)));
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, currentSize);
    
    app.setLastPage(app.getPages().size());
    app.getNumberOfPages().setText("" + app.getPages().size() + "pgs");
    
    int progress = (newCurrentPage)*100/app.getLastPage();
    
    app.getProgressTextView().setText("Progress " + progress + "%");
    int resid = getZoomP(app, textView);
    zoomText.setText("" + resid + "%");

  }
}
